package logic

import (
	"context"
	"cp/cache"
	"cp/cprpc/cprpc"
	"cp/cprpc/internal/svc"
	"cp/models/mysql"
	"cp/pkg"
	"errors"
	"github.com/zeromicro/go-zero/core/logx"
)

type RegisterLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewRegisterLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RegisterLogic {
	return &RegisterLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *RegisterLogic) Register(in *cprpc.RegisterRequest) (*cprpc.RegisterResponse, error) {
	// todo: add your logic here and delete this line
	result, _ := cache.Rdb.Get(l.ctx, "sendCode"+in.Phone).Result()
	if result != in.SendCode {
		return nil, errors.New("短信验证码错误,请重新输入")
	}

	var user = mysql.User{
		Phone:     in.Phone,
		Password:  pkg.Md5str(in.Password),
		NickName:  in.NickName,
		HeadImage: in.HeadImage,
		Status:    in.Status,
	}
	mysql.DB.Debug().Create(&user)

	return &cprpc.RegisterResponse{
		UserId: int64(user.ID),
	}, nil
}
